//
//  UIColor+FTX.m
//  FTXClient
//
//  Created by Lei Perry on 6/21/13.
//  Copyright (c) 2013 BitRice. All rights reserved.
//

#import "UIColor+EV.h"
#import <objc/runtime.h>

@implementation UIColor (EV)

+ (UIColor *)ev_colorWithHex:(NSUInteger)color
{
    return [UIColor ev_colorWithHex:color alpha:1];
}

+ (UIColor *)ev_colorWithHex:(NSUInteger)color alpha:(CGFloat)alpha
{
    unsigned char r, g, b;
    b = color & 0xFF;
    g = (color >> 8) & 0xFF;
    r = (color >> 16) & 0xFF;
    return [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:alpha];
}

+ (UIColor *) ev_colorWithHexString: (NSString *)stringToConvert alpha:(int)alpha
{
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) return [UIColor whiteColor];
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"] || [cString hasPrefix:@"0x"]) cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if ([cString length] != 6) return [UIColor whiteColor];
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha: (float)alpha/255.0];
}

+ (UIColor*)ev_colorWithColor:(UIColor*)color alpha:(CGFloat)alphaValue {
	CGFloat colorRed = 0.0;
	CGFloat colorGreen = 0.0;
	CGFloat colorBlue = 0.0;
	
	if (color != nil && [color getRed:&colorRed green:&colorGreen blue:&colorBlue alpha:nil]) {
		return [UIColor colorWithRed:colorRed green:colorGreen blue:colorBlue alpha:alphaValue];
	}
	return nil;
}

+ (UIColor *) ev_colorWithHexString: (NSString *) stringToConvert
{
    return [UIColor ev_colorWithHexString:stringToConvert alpha:255];
}

- (UIColor *)ev_colorByMixColor:(UIColor *)mixColor alpha:(CGFloat)alpha {
    CGFloat colorRed = 0.0;
    CGFloat colorGreen = 0.0;
    CGFloat colorBlue = 0.0;
    
    CGFloat mixColorRed = 0.0;
    CGFloat mixColorGreen = 0.0;
    CGFloat mixColorBlue = 0.0;
    
    if (mixColor != nil &&
        [self getRed:&colorRed green:&colorGreen blue:&colorBlue alpha:nil] &&
        [mixColor getRed:&mixColorRed green:&mixColorGreen blue:&mixColorBlue alpha:nil]) {
        return [UIColor colorWithRed:(colorRed * (1.0 - alpha) + mixColorRed * alpha)
                               green:(colorGreen * (1.0 - alpha) + mixColorGreen * alpha)
                                blue:(colorBlue * (1.0 - alpha) + mixColorBlue * alpha)
                               alpha:1.0];
    }
    return [self copy];
}

- (CGColorSpaceModel) colorSpaceModel
{
    return CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor));
}

- (CGFloat) ev_red
{
    const CGFloat *c = CGColorGetComponents(self.CGColor);
    return c[0];
}

- (CGFloat) ev_green
{
    const CGFloat *c = CGColorGetComponents(self.CGColor);
    if ([self colorSpaceModel] == kCGColorSpaceModelMonochrome) return c[0];
    return c[1];
}

- (CGFloat) ev_blue
{
    const CGFloat *c = CGColorGetComponents(self.CGColor);
    if ([self colorSpaceModel] == kCGColorSpaceModelMonochrome) return c[0];
    return c[2];
}

- (CGFloat) ev_alpha
{
    const CGFloat *c = CGColorGetComponents(self.CGColor);
    return c[CGColorGetNumberOfComponents(self.CGColor)-1];
}

- (UIColor *)ev_lighterColor {
    CGFloat r, g, b, a;
    if ([self getRed:&r green:&g blue:&b alpha:&a])
        return [UIColor colorWithRed:MIN(r + 0.2, 1.0)
                               green:MIN(g + 0.2, 1.0)
                                blue:MIN(b + 0.2, 1.0)
                               alpha:a];
    return nil;
}

- (UIColor *)ev_darkerColor {
    CGFloat r, g, b, a;
    if ([self getRed:&r green:&g blue:&b alpha:&a])
        return [UIColor colorWithRed:MAX(r - 0.2, 0.0)
                               green:MAX(g - 0.2, 0.0)
                                blue:MAX(b - 0.2, 0.0)
                               alpha:a];
    return nil;
}

- (NSString *)ev_hexString {
    CGFloat colorRed = 0.0;
    CGFloat colorGreen = 0.0;
    CGFloat colorBlue = 0.0;
    
    [self getRed:&colorRed green:&colorGreen blue:&colorBlue alpha:nil];
    return [NSString stringWithFormat:@"%x%x%x", (int)(colorRed * 0xFF), (int)(colorGreen * 0xFF), (int)(colorBlue * 0xFF)];
}

@end

@implementation UIColor (ChargerLink)

+ (UIColor *)ev_majorYellowColor {
    return [UIColor ev_colorWithHex:0xFCA927];
}

+ (UIColor *)ev_minorYellowColor {
    return [UIColor ev_colorWithHex:0xFCCC27];
}

#pragma mark - UI Standard Colors (0606)
+ (UIColor *)ev_majorRedColor {
    return [UIColor ev_colorWithHex:0xe65454];
}

+ (UIColor *)ev_majorRedColor_pressed {
    return [UIColor ev_colorWithHex:0xd44a4a];
}

+ (UIColor *)ev_majorBackgroundColor {
    return [UIColor ev_colorWithHex:0xf0f0f0];
}

+ (UIColor *)ev_minorBackgroundColor {
    return [UIColor ev_colorWithHex:0xfafafa];
}

+ (UIColor *)ev_darkBackgroundColor {
    return [UIColor ev_colorWithHex:0x44546a];
}

+ (UIColor *)ev_darkBackgroundColor_pressed {
    return [UIColor ev_colorWithHex:0x374457];
}

+ (UIColor *)ev_whiteBackgroundColor_pressed {
    return [UIColor ev_colorWithHex:0xf5f5f5];
}

+ (UIColor *)ev_majorLineColor {
    return [UIColor ev_colorWithHex:0xd6d6d6];
}

+ (UIColor *)ev_minorLineColor {
    return [UIColor ev_colorWithHex:0xe5e5e5];
}

+ (UIColor *)ev_darkLineColor {
    return [UIColor ev_colorWithHex:0xcccccc];
}

#pragma mark UI Standard Text Colors (0606)
+ (UIColor *)ev_majorTextColor {
    return [UIColor ev_colorWithHex:0x333333];
}

+ (UIColor *)ev_normalTextColor {
    return [UIColor ev_colorWithHex:0x666666];
}

+ (UIColor *)ev_minorTextColor {
    return [UIColor ev_colorWithHex:0x999999];
}

+ (UIColor *)ev_lightTextColor {
    return [UIColor ev_colorWithHex:0xb2b2b2];
}

+ (UIColor *)ev_linkTextColor {
    return [UIColor ev_blueLinkColor];
}

+ (UIColor *)ev_blueLinkColor {
    return [UIColor ev_colorWithHex:0x576a95];
}

+ (UIColor *)ev_redTextColor {
    return [UIColor ev_colorWithHex:0xe85352];
}

#pragma mark - Other Colors

+ (UIColor *)ev_textFieldBackgroundColor
{
    return [UIColor ev_colorWithHex:0xf5f5f5];
}

+ (UIColor *)ev_buttonNormalRedBgColor
{
    return [UIColor ev_colorWithHex:0xe85352];

}
+ (UIColor *)ev_buttonPressedRedBgColor
{
    return [UIColor ev_colorWithHex:0xd44a4a];

}
+ (UIColor *)ev_buttonDisableGrayBgColor
{
     return [UIColor ev_colorWithHex:0xe6e6e6];
}
+ (UIColor *)ev_buttonDisalbleGrayTextColor
{
    return [UIColor ev_colorWithHex:0xb2b2b2];
}
+ (UIColor *)ev_buttonPressedGrayBgColor
{
    return [UIColor ev_colorWithHex:0xf6f6f6];
}

+ (UIColor *)ev_maskBackgroundColor {
    return [UIColor colorWithWhite:0.0 alpha:0.3];
}

@end

@implementation UIColor(UIColor_patternArchive)
static char STRING_KEY;

+ (void)load {
    // Swizzle UIColor encodeWithCoder:
    Method encodeWithCoderAssociatedObject = class_getInstanceMethod([UIColor class], @selector(encodeWithCoderAssociatedObject:));
    Method encodeWithCoder = class_getInstanceMethod([UIColor class], @selector(encodeWithCoder:));
    method_exchangeImplementations(encodeWithCoder, encodeWithCoderAssociatedObject);
    
    // Swizzle UIColor initWithCoder:
    Method initWithCoderAssociatedObject = class_getInstanceMethod([UIColor class], @selector(initWithCoderAssociatedObject:));
    Method initWithCoder = class_getInstanceMethod([UIColor class], @selector(initWithCoder:));
    method_exchangeImplementations(initWithCoder, initWithCoderAssociatedObject);
}

- (UIImage *)image {
    return objc_getAssociatedObject(self, &STRING_KEY);
}

- (void)setImage:(UIImage *)image {
    objc_setAssociatedObject(self, &STRING_KEY, image, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)encodeWithCoderAssociatedObject:(NSCoder *)aCoder
{
    if (CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor))==kCGColorSpaceModelPattern)
    {
        UIImage *i = [UIImage imageNamed:@"read_bg"];
        NSData *imageData = UIImagePNGRepresentation(i);
        [aCoder encodeObject:imageData forKey:@"associatedObjectKey"];
    } else {
        // Call default implementation, Swizzled
        [self encodeWithCoderAssociatedObject:aCoder];
    }
}

- (id)initWithCoderAssociatedObject:(NSCoder *)aDecoder
{
    if([aDecoder containsValueForKey:@"associatedObjectKey"])
    {
        UIImage *i = [UIImage imageNamed:@"read_bg"];
        self = [UIColor colorWithPatternImage:i];
        [self setImage:i];
        return self;
    }
    else
    {
        // Call default implementation, Swizzled
        return [self initWithCoderAssociatedObject:aDecoder];
    }
}

@end

